import {BaseFoe} from "../BaseFoe.js";
import {Container} from "../../container/Container.js";
import {Tool} from "../../tools/Tool.js";
import {Log} from "../../log/Log.js";
import {CompeteConstant} from "../../constants/CompeteConstant.js";

export class Foe extends BaseFoe{
    class = '';
    _startSit = [];//初始位置
    defaultDirection = 'down';
    constructor() {
        super();
    }

    /**
     * 初始化
     */
    init(){
        this._beforeCheck();
        $(this.class).attr('compete', CompeteConstant.FOE);

    }

    /**
     * 前置检查
     * @private
     */
    _beforeCheck(){
        if(!this.class) throw new Error('class必须');

    }

    /**
     * 获取所有敌军对象
     * @returns {*|jQuery.fn.init|jQuery|HTMLElement}
     */
    getFoeObjects(){
        return $('div[compete="'+CompeteConstant.FOE+'"]')
    }

    /**
     * 设置昵称
     * @param nickname
     */
    setNickname(nickname){
        $(this.class).attr('nickname', nickname?nickname:this._getNickname());
    }

    /**
     * 简要
     * @param title
     */
    setTitle(title){
        $(this.class).attr('title', title?title:this.title);
    }

    getTitle(title){
        $(this.class).attr('title');
    }

    /**
     * 描述
     * @param desc
     */
    setDescription(desc){
        $(this.class).attr('description', desc?desc:this.description);
    }

    getDescription(){
        $(this.class).attr('description');
    }

    /**
     * 获取昵称
     * @returns {jQuery}
     */
    getNickname(){
        return $(this.class).attr('nickname');
    }

    /**
     *
     * @param len
     * @returns {string}
     * @private
     */
    _getNickname(len){
        return this.compete +'_'+ Tool.randomLetterNumber(len);
    }

    /**
     * 设置class名
     * @param cls
     */
    setClass(cls){
        this.class = '.'+cls;
    }

    /**
     * 获取class
     * @returns {string}
     */
    getClass(){
        return this.class;
    }


    /**
     * 创建敌军控件
     * 注：css需要开发者手动完成
     */
    createFoe(){

        let htm = '<div class="'+(this.class.replace('.',''))+'"></div>';

        $(Container.id).append(htm);
        Log.log('控件css样式由开发者自由控制');
    }

    /**
     * 设置初始位置
     */
    setStartSit(sit){
        if(sit && !Array.isArray(sit)) throw new Error('参数必须是数组.eg:[{top:100,left:0},{top:100,left:50},{top:100,left:150}]');
        this._startSit = sit;

    }
    /**
     * 创建敌军控件
     * 注：css需要开发者手动完成
     */
    createFoes(foes){
        if(this._startSit && foes !== this._startSit.length) throw new Error('初始位置数量必须与创建Foes控件数量一致');
        let htm = '';
        let nickname = '';
        let id = '';
        let top = '';
        let left = '';
        let idBox = [];
        for (let i=0; i<foes; i++){
            nickname = this._getNickname(5);//临时昵称
            id = nickname+'_'+i+'_'+Tool.getNowTime();//组装唯一ID
            htm = '<div class="'+(this.class.replace('.',''))+'" nickname="'+nickname+'" compete="'+this.compete+'" id="'+id+'"></div>';
            $(Container.id).append(htm);

            //初始位置
            if(!this._startSit){
                //随机
                top += 50;
                left += this.getWidth($(id));
            }else{
                //使用给定位置
                top = this._startSit[i].top;
                left = this._startSit[i].left;
            }
            idBox.push(id);
            let tmpObj = $('#'+id);
            $(tmpObj).css({'top':top, left:left});//设置默认坐标
            this.setDirection(tmpObj, this.defaultDirection);//设置默认方位
        }
        Log.log('控件css样式由开发者自由控制');
        return idBox;
    }


}